﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinQProject;
using ServiceProject.EntityService;

public partial class controls_ucConversation : System.Web.UI.UserControl
{

    private static FarmacieDataClassesDataContext database = new FarmacieDataClassesDataContext();

    protected ConversationService convService = new ConversationService(database);
    protected ConversationDetailsService convDetailsService = new ConversationDetailsService(database);
    protected UserService userService = new UserService(database);

    public int ProductId
    {
        get
        {
            int result;
            if (Int32.TryParse(hdnProductId.Value, out result))
            {
                return result;
            }
            return 0;
        }
        set { hdnProductId.Value = value.ToString(); }
    }

    public User UserItem { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Session["username"].ToString()))
            {
                User user = userService.GetUserByUserName(Session["username"].ToString());
                UserItem = user;
                CompleteNumePrenume(user);
            }
        }
    }

    private void CompleteNumePrenume(User user)
    {
        if (user == null)
            txtNumePrenume.Text = string.Empty;
        else if (user.Clients[0] != null)
            txtNumePrenume.Text = user.Clients[0].ClientName;
        else
            txtNumePrenume.Text = string.Empty;
    }

    protected void btnAskId_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(txtNumePrenume.Text))
        {
            lblEroare.Text = "Completati Numele si Prenumele";
            return;
        }
        if (String.IsNullOrEmpty(txtAsk.Text))
        {
            lblEroare.Text = "Completati Intrebarea";
            return;
        }

        Conversation conversation = new Conversation
                                        {
                                            AnonimName = txtNumePrenume.Text,
                                            ConversationDate = DateTime.Now,
                                            IsActive = true
                                        };
        if (ProductId != 0) conversation.ProductId = ProductId;
        if (UserItem != null) conversation.UserId = UserItem.UserId;

        int idConversation;
        Exception eroare;
        if (convService.InsertUpdate(conversation,out idConversation,out eroare))
        {
            ConversationDetail convDetail=new ConversationDetail
                                              {
                                                  ConversationId = idConversation,
                                                  DetailAnonimName = txtNumePrenume.Text,
                                                  DetailDate = conversation.ConversationDate,
                                                  DetailText = txtAsk.Text,
                                                  DetailUserId = conversation.UserId,
                                                  IsActive = true
                                              };
            int convDetailId=0;
            Exception error;
            if (convDetailsService.InsertUpdate(convDetail,out convDetailId,out error))
            {
                lblEroare.Text = "Intrebare plasata cu succes";
            }
            else
            {
                lblEroare.Text = "Intrebarea nu a putut fi salvata. Va rugam sa reveniti.";
            }
        }
        else
        {
            lblEroare.Text = "Intrebarea nu a putut fi salvata. Va rugam sa reveniti.";
        }
        ClearAll();
        OnSave(sender,e);
    }

    private void ClearAll()
    {
        ProductId = 0;
        txtNumePrenume.Text = string.Empty;
        txtAsk.Text = string.Empty;
    }

    #region Delegates && Events
    public delegate void SaveEventHandler(object sender, EventArgs e);
    public event SaveEventHandler Save;

    protected void OnSave(object sender, EventArgs e)
    {
        if (Save != null)
        {
            Save(sender, e);
        }
    }
    #   endregion
}
